<?php

/**
 * account actions.
 *
 * @package    sf_sandbox
 * @subpackage account
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class accountActions extends sfActions {
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	
	public function executePayvip(sfWebRequest $request) {
		
		$summa = $request->getParameter('vip[days]')*sfConfig::get('app_cost_vip');
		if($this->getUser()->getProfile()->getAccount()>$summa)
		{
			$p = new Payment ( );
			$p->setLevelId ( 12 );
			$p->setSumma ( $summa );
			$p->setUserId ( $this->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ) );
			$p->setPaymentId ( $this->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ) );
			$p->setDateExpire ( $p->countDateExpire ( 12, $request->getParameter('vip[days]') ) );
			$p->save();	
			$pf = $this->getUser ()->getProfile();
			$pf->setAccount($pf->getAccount()-$summa);
			$pf->setIsVip(true);
			$pf->setIsTop(true);
			$pf->save();	
			$this->getUser ()->setFlash ( 'message', __('Оплата успешно выполнена') );
		}
		else 
		{
		  $this->getUser ()->setFlash ( 'error', __('У вас недостаточно средств на счете!') );
		}
		$this->redirect('account/index');
		
	}
	
	
	public function executeIndex(sfWebRequest $request) {

       
		$this->vip_form = new VipForm();
		
		 // create paypal library instance
      $gateway = new sfPaymentPayPal();

      $art = array(
      '30' => 'payment #member30# user#'.$this->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ).'#',
      '60' => 'payment #member60# user#'.$this->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ).'#',
      '90' => 'payment #member90# user#'.$this->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ).'#',
      '180' => 'payment #member90# user#'.$this->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ).'#',
      '360' => 'payment #member90# user#'.$this->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ).'#',
      '38' => 'payment #member90# user#'.$this->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ).'#',
      '73' => 'payment #member90# user#'.$this->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ).'#',
      '100' => 'payment #member90# user#'.$this->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ).'#',
      );
      
      foreach($art as $k=>$v)
      {
      // instanciate transaction
      $str = 'transaction'.$k;
      $this->$str = new sfPaymentTransaction($gateway);
      $this->$str->enableTestMode();
      $this->$str->setCurrency("USD");
      $this->$str->setAmount($k);
      $this->$str->setProductName($v);
      }
		
		
		$this->form = new PaymentForm ( );
		
		$c = new Criteria ( );
		$c->add ( PaymentPeer::PAYMENT_ID, $this->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ) );
		$c->addDescendingOrderByColumn ( PaymentPeer::ID );

    	if(!$request->getParameter('page'))
  	    {
  	    	$this->page = 1;
  	    }
  	    else 
  	    {
  	    	$this->page=$request->getParameter('page');
  	    } 		
		
		$pager = new sfPropelPager ( 'Payment', sfConfig::get ( 'app_payment_perpage' ) );
		$pager->setCriteria ( $c );
		$pager->setPeerMethod ( 'doSelect' );
		$pager->setPage ( $this->page );
		$pager->init ();
		$this->payment = $pager;
		//Кидаем пользователя в layout
	     $this->getRequest()->setAttribute('v', $this->getUser()->getProfile());
		////
	
	}
	
	public function executeAdd(sfWebRequest $request) {
		
		$this->form = new PaymentForm ( );
		
		if ($request->isMethod ( 'post' )) 

		{
			
			$this->form->bind ( $request->getParameter ( 'payment' ), $request->getFiles ( 'payment' ) );
			if ($this->form->isValid ()) {
				
				$this->form->save ();
				$user = sfGuardUserPeer::retrieveByPK ( $this->form->getObject ()->getUserId () );
				$user->setAccount ( $user->getAccount () + $this->form->getObject ()->getSumma () );
				$user->save ();
				$this->redirect ( '@account' );
			}
		
		}
	
	}
	
	public function executePay(sfWebRequest $request) {
		
		if ($request->getParameter ( 'level' ) and $request->getParameter ( 'period' )) {
			$p = new Payment ( );
			$p->setUserId ( $this->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ) );
			$p->setPaymentId ( $this->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ) );
			$user = $this->getUser ()->getProfile ();
			switch ($request->getParameter ( 'level' )) {
				case 3 :
					$p->setLevelId ( 3 );
					switch ($request->getParameter ( 'period' )) {
						case 1 :
							if (sfConfig::get ( 'app_cost_abon_1' ) <= $user->getAccount ()) {
								$p->setSumma ( sfConfig::get ( 'app_cost_abon_1' ) );
								$p->setDateExpire ( $p->countDateExpire ( 3, 1 ) );
								$p->save ();
								//$user->setAccount ( $user->getAccount () - sfConfig::get ( 'app_cost_abon_1' ) );
								$user->setIsAbon(true);
								$user->setDateExpireAbon($p->countDateExpire ( 3, 1 ));
								$user->save ();
								$this->getUser ()->setFlash ( 'message', __('Оплата успешно выполнена') );
							} else {
								$this->getUser ()->setFlash ( 'error', __('У вас недостаточно средств на счете!') );
							}
							break;
						case 2 :
							if (sfConfig::get ( 'app_cost_abon_2' ) <= $user->getAccount ()) {
								$p->setSumma ( sfConfig::get ( 'app_cost_abon_2' ) );
								$p->setDateExpire ( $p->countDateExpire ( 3, 2 ) );
								$p->save ();
								//$user->setAccount ( $user->getAccount () - sfConfig::get ( 'app_cost_abon_2' ) );
								$user->setIsAbon(true);
								$user->setDateExpireAbon($p->countDateExpire ( 3, 2 ));
								$user->save ();
								$this->getUser ()->setFlash ( 'message', __('Оплата успешно выполнена') );
							} else {
								$this->getUser ()->setFlash ( 'error', __('У вас недостаточно средств на счете!') );
							}
							break;
						case 3 :
							if (sfConfig::get ( 'app_cost_abon_3' ) <= $user->getAccount ()) {
								$p->setSumma ( sfConfig::get ( 'app_cost_abon_3' ) );
								$p->setDateExpire ( $p->countDateExpire ( 3, 3 ) );
								$p->save ();
								//$user->setAccount ( $user->getAccount () - sfConfig::get ( 'app_cost_abon_3' ) );
								$user->setIsAbon(true);
								$user->setDateExpireAbon($p->countDateExpire ( 3, 3 ));
								$user->save ();
								$this->getUser ()->setFlash ( 'message', __('Оплата успешно выполнена') );
							} else {
								$this->getUser ()->setFlash ( 'error', __('У вас недостаточно средств на счете!') );
							}
							break;
						
						case 6 :
							if (sfConfig::get ( 'app_cost_abon_6' ) <= $user->getAccount ()) {
								$p->setSumma ( sfConfig::get ( 'app_cost_abon_6' ) );
								$p->setDateExpire ( $p->countDateExpire ( 3, 6 ) );
								$p->save ();
								//$user->setAccount ( $user->getAccount () - sfConfig::get ( 'app_cost_abon_6' ) );
								$user->setIsAbon(true);
								$user->setDateExpireAbon($p->countDateExpire ( 3, 6 ));
								$user->save ();
								$this->getUser ()->setFlash ( 'message', __('Оплата успешно выполнена') );
							} else {
								$this->getUser ()->setFlash ( 'error', __('У вас недостаточно средств на счете!') );
							}
							break;
						case 12 :
							if (sfConfig::get ( 'app_cost_abon_12' ) <= $user->getAccount ()) {
								$p->setSumma ( sfConfig::get ( 'app_cost_abon_12' ) );
								$p->setDateExpire ( $p->countDateExpire ( 3, 12 ) );
								$p->save ();
								//$user->setAccount ( $user->getAccount () - sfConfig::get ( 'app_cost_abon_12' ) );
								$user->setIsAbon(true);
								$user->setDateExpireAbon($p->countDateExpire ( 3, 12 ));
								$user->save ();
								$this->getUser ()->setFlash ( 'message', __('Оплата успешно выполнена') );
							} else {
								$this->getUser ()->setFlash ( 'error', __('У вас недостаточно средств на счете!') );
							}
							break;
					}
					
					break;
				case 4 :
					$p->setLevelId ( 4 );
					switch ($request->getParameter ( 'period' )) {
						case 10 :
							
							if (sfConfig::get ( 'app_cost_top_10' ) <= $user->getAccount ()) {
								$p->setSumma ( sfConfig::get ( 'app_cost_top_10' ) );
								$p->setDateExpire ( $p->countDateExpire ( 4, 10 ) );
								$p->save ();
								$user->setAccount ( $user->getAccount () - sfConfig::get ( 'app_cost_top_10' ) );
								$user->setIsTop(true);
								$user->save ();
								$this->getUser ()->setFlash ( 'message', __('Оплата успешно выполнена') );
							} else {
								$this->getUser ()->setFlash ( 'error', __('У вас недостаточно средств на счете!') );
							}
							
							break;
						case 20 :
							if (sfConfig::get ( 'app_cost_top_20' ) <= $user->getAccount ()) {
								$p->setSumma ( sfConfig::get ( 'app_cost_top_20' ) );
								$p->setDateExpire ( $p->countDateExpire ( 4, 20 ) );
								$p->save ();
								$user->setAccount ( $user->getAccount () - sfConfig::get ( 'app_cost_top_20' ) );
								$user->setIsTop(true);
								$user->save ();
								$this->getUser ()->setFlash ( 'message', __('Оплата успешно выполнена') );
							} else {
								$this->getUser ()->setFlash ( 'error', __('У вас недостаточно средств на счете!') );
							}
							break;
						case 30 :
							if (sfConfig::get ( 'app_cost_top_30' ) <= $user->getAccount ()) {
								$p->setSumma ( sfConfig::get ( 'app_cost_top_30' ) );
								$p->setDateExpire ( $p->countDateExpire ( 4, 30 ) );
								$p->save ();
								$user->setAccount ( $user->getAccount () - sfConfig::get ( 'app_cost_top_30' ) );
								$user->setIsTop(true);
								$user->save ();
								$this->getUser ()->setFlash ( 'message', __('Оплата успешно выполнена') );
							} else {
								$this->getUser ()->setFlash ( 'error', __('У вас недостаточно средств на счете!') );
							}
							break;
					}
					
					break;
			
			}
		}
		
		$this->redirect ( '@account' );
	
	}
	
	
	/*
	 *  Paypall
	 */
	public function executePpsuccess(sfWebRequest $request) {
		
		
	}
	public function executePpcancel(sfWebRequest $request) {
		
	}
	public function executePpnotify(sfWebRequest $request) {
		
	}
	
	
}
